/*
 * Copyright 2022 The Open Islands Authors. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.openislands.oi.pojo.vo;

import org.openislands.oi.constant.MessageCodeEnum;
import org.openislands.oi.pojo.dto.CommonResult;

import java.io.Serializable;
import java.util.Objects;

public class CommonResponse<E> implements Serializable {
    private int code;
    private String message;
    private String errorMessage;
    private E data;

    public CommonResponse() {
    }

    public CommonResponse(int code, String message) {
        this.code = code;
        this.message = message;
    }

    public CommonResponse(int code, String message, E data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public E getData() {
        return data;
    }

    public void setData(E data) {
        this.data = data;
    }

    public String getErrorMessage() {
        return errorMessage;
    }

    public void setErrorMessage(String errorMessage) {
        this.errorMessage = errorMessage;
    }

    public boolean responseSuccess() {
        return this.code == MessageCodeEnum.SUCCESS.getCode();
    }

    @Override
    public String toString() {
        return "CommonResponse{" +
                "code=" + code +
                ", message='" + message + '\'' +
                ", errorMessage='" + errorMessage + '\'' +
                ", data=" + data +
                '}';
    }

    public static <E> CommonResponse<E> byMessageCode(MessageCodeEnum code, E data) {
        if (Objects.isNull(code)) code = MessageCodeEnum.SYSTEM_ERROR;
        return new CommonResponse<>(code.getCode(), code.getMessage(), data);
    }

    public static <E> CommonResponse<E> success(E data) {
        return byMessageCode(MessageCodeEnum.SUCCESS, data);
    }

    public static <E> CommonResponse<E> success() {
        return success(null);
    }

    public static <E> CommonResponse<E> error(MessageCodeEnum messageCode, E data) {
        return byMessageCode(messageCode, data);
    }

    public static <E> CommonResponse<E> error(MessageCodeEnum messageCode) {
        return error(messageCode, null);
    }

    public static <E> CommonResponse<E> error(String errorMessage, MessageCodeEnum messageCode) {
        final CommonResponse<E> commonResponse = error(messageCode);
        commonResponse.setErrorMessage(errorMessage);
        return commonResponse;
    }

    public static <E> CommonResponse<E> parse(CommonResult<E> commonResult) {
        return parse(commonResult, commonResult.getData());
    }

    public static <E, F> CommonResponse<E> parse(CommonResult<F> commonResult, E data) {
        final CommonResponse<E> commonResponse = byMessageCode(commonResult.getCode(), data);
        commonResponse.setErrorMessage(commonResult.getMessage());
        return commonResponse;
    }
}
